<!--
 * @Description: 
 * @version: 
 * @Author: liurui
 * @Date: 2024-02-18 15:59:09
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-02-18 16:34:11
-->
<template>
    <div>

    </div>
</template>

<script setup lang="ts">
// 实现一个类型ReplaceKeys，用于替换联合类型中的键，
// 如果某个类型没有此键，则跳过替换，一个类型需要三个参数。
type NodeA = {
  type: 'A';
  name: string;
  flag: number;
};

type NodeB = {
  type: 'B';
  id: number;
  flag: number;
};

type NodeC = {
  type: 'C';
  name: string;
  flag: number;
};

type Nodes = NodeA | NodeB | NodeC;

type ReplacedNodes = ReplaceKeys<
  Nodes,
  'name' | 'flag',
  { name: number; flag: string }
>; 
// {type: 'A', name: number, flag: string} | {type: 'B', id: number, flag: string} | {type: 'C', name: number, flag: string} 
// would replace name from string to number, replace flag from number to string.

type ReplacedNotExistKeys = ReplaceKeys<Nodes, 'name', { aa: number }>; 
// {type: 'A', name: never, flag: number} | NodeB | {type: 'C', name: never, flag: number} 
// would replace name to never

type ReplaceKeys<T, K, U> = T extends any
  ? { [P in keyof T]: P extends K ? (P extends keyof U ? U[P] : never) : T[P] } 
  : never;
</script>

<style scoped>

</style>